﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SharpShare.Storage {
    public class StorageItemCollection : IList<IStorageItem>, IStorageLookup {
        private StorageItemKeyedCollection _innerList = new StorageItemKeyedCollection();
        private object _lock = new object();

        public bool Contains(string name) {
            return (this[name] != null);
        }

        #region IStorageLookup Members

        public IStorageItem this[string name] {
            get {
                lock (_lock) {
                    if (_innerList.Contains(name)) {
                        return _innerList[name];
                    }

                    return null;
                }
            }
        }

        #endregion

        #region IList<IStorageItem> Members

        public int IndexOf(IStorageItem item) {
            lock (_lock) {
                return _innerList.IndexOf(item);
            }
        }

        public void Insert(int index, IStorageItem item) {
            lock (_lock) {
                _innerList.Insert(index, item);
            }
        }

        public void RemoveAt(int index) {
            lock (_lock) {
                _innerList.RemoveAt(index);
            }
        }

        public IStorageItem this[int index] {
            get {
                lock (_lock) {
                    return _innerList[index];
                }
            }
            set {
                lock (_lock) {
                    _innerList[index] = value;
                }
            }
        }

        #endregion

        #region ICollection<IStorageItem> Members

        public void Add(IStorageItem item) {
            lock (_lock) {
                _innerList.Add(item);
            }
        }

        public void Clear() {
            lock (_lock) {
                _innerList.Clear();
            }
        }

        public bool Contains(IStorageItem item) {
            lock (_lock) {
                return _innerList.Contains(item);
            }
        }

        public void CopyTo(IStorageItem[] array, int arrayIndex) {
            lock (_lock) {
                _innerList.CopyTo(array, arrayIndex);
            }
        }

        public int Count {
            get {
                lock (_lock) {
                    return _innerList.Count;
                }
            }
        }

        public bool IsReadOnly {
            get { return false; }
        }

        public bool Remove(IStorageItem item) {
            lock (_lock) {
                return _innerList.Remove(item);
            }
        }

        #endregion

        #region IEnumerable<IStorageItem> Members

        public IEnumerator<IStorageItem> GetEnumerator() {
            IStorageItem[] allItems;

            lock (_lock) {
                allItems = new IStorageItem[_innerList.Count];
                _innerList.CopyTo(allItems, 0);
            }

            return ((IEnumerable<IStorageItem>)allItems).GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return this.GetEnumerator();
        }

        #endregion

        private sealed class StorageItemKeyedCollection : KeyedCollection<string, IStorageItem> {
            public StorageItemKeyedCollection()
                : base(StringComparer.OrdinalIgnoreCase, -1) {

            }

            protected override string GetKeyForItem(IStorageItem item) {
                return item.Name;
            }
        }
    }
}
